package General;

import java.util.Collections;
import java.util.LinkedList;
import java.util.Observable;

public class Bank extends Observable{
	public static Bank singletonBank = null;
	private final int NUM_OF_VICTORY=4;
	private Wallet bankWallet;
	private int[] victoryCards;
	private final int[] startRes = {0,0,20,25,30,40,50,55,60};
	private final int STARTING_VICTORY = 30;
	private int victoryPts;
	private LinkedList<Tile> tilePool;
	private Bank(int playerCount){
		bankWallet = new Wallet(new Resource(startRes[playerCount],startRes[playerCount],startRes[playerCount],startRes[playerCount],STARTING_VICTORY));//starting resources
		victoryCards = new int[NUM_OF_VICTORY];	//initialize victory cards
		victoryPts= STARTING_VICTORY;
		tilePool = Culture.getTileSet();
	}
	public void placeVictory(VictoryCards card){
		victoryCards[card.ordinal()]++;
		victoryPts--;
		setChanged();
		notifyObservers(new Integer(victoryPts));
	}
	public void rewardBattle(Player target){	//this victory card is unique in that it is called while the game is running.
		target.recieveVictory(victoryCards[VictoryCards.WON_LAST_BATTLE.ordinal()]);
		victoryCards[VictoryCards.WON_LAST_BATTLE.ordinal()]=0;
	}
	public static Bank createBank(int playerCount){
		if(singletonBank == null){
			singletonBank = new Bank(playerCount);
		}
		return singletonBank;
	}
	public Tile getTile(){
		return tilePool.pop();
	}
	public void rePoolTile(Tile tile){
		tilePool.add(tile);
		Collections.shuffle(tilePool);
	}
	public Wallet getWallet(){	//not good who uses this method?
		return bankWallet;
	}
	//receive into the bank and payment to the user
	public boolean tradeResource(Resource receiving, Resource payment, Player aPlayer){
		if (bankWallet.myResources.creditCheck(payment)){
			bankWallet.forcePay(payment, aPlayer.getWallet());
			bankWallet.receive(receiving);
			aPlayer.getWallet().pay(receiving, bankWallet);
			aPlayer.getWallet().receive(payment);
			return true;
		}
		return false;
	}
	public void purchaseVictory(int point, Player aPlayer){
		victoryPts -= point;
		aPlayer.recieveVictory(point);
	}
	public int getVictory(VictoryCards vc){
		return victoryCards[vc.ordinal()];
	}
}
